package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.work.keep.R;
import com.work.keep.adapter.RecommendAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.RecommendData;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/14.
 */
public class RecommendActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private String recommd = "https://api.gotokeep.com/v1.1/home/recommend";
    private RecommendAdapter recommendAdapter;
    private ImageView recommendLeft;

    public static void goToRecommendActivity(Context context) {
        Intent intent = new Intent(context, RecommendActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_recommend_recycler;
    }

    @Override
    protected void initView() {
        recommendLeft = (ImageView) findViewById(R.id.recommendLeft);
        recyclerView = (RecyclerView) findViewById(R.id.recommendRecycler);
    }

    @Override
    protected void initData() {
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        recommendAdapter = new RecommendAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(recommendAdapter);
        recommendLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        GsonRequest<RecommendData> recommdDataGsonRequest = new GsonRequest<RecommendData>(recommd, RecommendData.class, new Response.Listener<RecommendData>() {
            @Override
            public void onResponse(RecommendData response) {
                recommendAdapter.addData(response.getData().getPlans(), response.getData().getWorkouts());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzM2MTg5MCwiZXhwIjoxNDU1OTUzODkwLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.m7C0yFB4b6-2oRx5SDrVOk4T8exoWFxJ-oJHyRh1ckU");
                map.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                map.put("Host", "api.gotokeep.com");
                return map;
            }
        };
        volleySingleton._addRequest(recommdDataGsonRequest);
    }
}
